//
//  YAYISchedulingCell.m
//  testForAreacode
//
//  Created by Veer on 16/6/4.
//  Copyright © 2016年 Veer. All rights reserved.
//

#import "YAYISchedulingCell.h"

#import "YAYISchedulingItemModel.h"


@interface YAYISchedulingCell()

@property (nonatomic, weak) YAYISchedulingView *morningSchedulingView;
@property (nonatomic, weak) YAYISchedulingView *afternoonSchedulingView;
@property (nonatomic, weak) UIView *vLineView1;
@property (nonatomic, weak) UIView *hLineView1;
@property (nonatomic, weak) UIView *vLineView2;
@property (nonatomic, weak) UIView *vLineView3;
@property (nonatomic, weak) UIView *vLineView4;
@property (nonatomic, weak) UIView *vLineView5;
@property (nonatomic, weak) UIView *hLineView2;
@property (nonatomic, weak) UIView *hLineView3;
@property (nonatomic, weak) UIView *vLineView6;

@end

@implementation YAYISchedulingCell


- (void)setLineType:(LineType)lineType
{
    _lineType = lineType;
    if (LineTypeFirst == lineType) {
        self.vLineView1.hidden = NO;
        self.vLineView3.hidden = NO;
        self.vLineView5.hidden = NO;
        self.morningSchedulingView.lineType = LineTypeFirst;
        self.afternoonSchedulingView.lineType = LineTypeFirst;
    }else{
        self.vLineView1.hidden = YES;
        self.vLineView3.hidden = YES;
        self.vLineView5.hidden = YES;
        self.morningSchedulingView.lineType = LineTypeNormal;
        self.afternoonSchedulingView.lineType = LineTypeNormal;
    }
}


- (void)setModel:(YAYISchedulingItemModel *)model
{
    _model = model;
    
    
    // 设置上午和下午排班的数据
    self.morningSchedulingView.isOpen = [model.flagArray[0] integerValue];
    self.morningSchedulingView.isExpired = model.isExpired;;
    
    self.afternoonSchedulingView.isOpen = [model.flagArray[1] integerValue];
    self.afternoonSchedulingView.isExpired = model.isExpired;
    
}


+ (instancetype)cellWith:(UITableView *)tablevie
{
    static NSString *cellID = @"YAYIEditPersonalInfoCell";
    
    YAYISchedulingCell *cell = [tablevie dequeueReusableCellWithIdentifier:cellID];
    
    if (nil == cell) {
        cell = [[YAYISchedulingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        
    }
    return cell;
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupSubView];
    }
    return  self;
}


- (void)setupSubView
{
    // 上午排班
    YAYISchedulingView *morningSchedulingView = [[YAYISchedulingView alloc] init];//WithFrame:CGRectMake(0, itemH, schedulingW, schedulingH)];
    morningSchedulingView.itemClickOperation = ^(BOOL isOpen){
    
        if ([self.delegate respondsToSelector:@selector(schedulingCellItemViewDidCickAtIndex:withIndexPath:state:)]) {
            return [self.delegate schedulingCellItemViewDidCickAtIndex:0 withIndexPath:self.indexPath state:isOpen];
        }else{
            return YES;
        }
    };
    
    [self.contentView addSubview:morningSchedulingView];
    self.morningSchedulingView = morningSchedulingView;
    
    // 各种线
    UIView *vLineView1 = [[UIView alloc] init];
    vLineView1.backgroundColor = YAYIColor(180, 180, 180);
    [self.contentView addSubview:vLineView1];
    self.vLineView1 = vLineView1;
    
    UIView *hLineView1 = [[UIView alloc] init];
    hLineView1.backgroundColor = YAYIColor(180, 180, 180);
    [self.contentView addSubview:hLineView1];
    self.hLineView1 = hLineView1;
    
    UIView *vLineView2 = [[UIView alloc] init];
    vLineView2.backgroundColor = YAYIColor(180, 180, 180);
    [self.contentView addSubview:vLineView2];
    self.vLineView2 = vLineView2;
    
    UIView *vLineView3 = [[UIView alloc] init];
    vLineView3.backgroundColor = YAYIColor(180, 180, 180);
    [self.contentView addSubview:vLineView3];
    self.vLineView3 = vLineView3;
    
    UIView *vLineView4 = [[UIView alloc] init];
    vLineView4.backgroundColor = YAYIColor(180, 180, 180);
    [self.contentView addSubview:vLineView4];
    self.vLineView4 = vLineView4;
    
    // 下午排班
    YAYISchedulingView *afternoonSchedulingView = [[YAYISchedulingView alloc] init];//WithFrame:CGRectMake(0, itemH, schedulingW, schedulingH)];
    afternoonSchedulingView.itemClickOperation = ^(BOOL isOpen){
        
        if ([self.delegate respondsToSelector:@selector(schedulingCellItemViewDidCickAtIndex:withIndexPath:state:)]) {
            return [self.delegate schedulingCellItemViewDidCickAtIndex:1 withIndexPath:self.indexPath state:isOpen];
        }else{
            return YES;
        }
    };
    [self.contentView addSubview:afternoonSchedulingView];
    self.afternoonSchedulingView = afternoonSchedulingView;
    
    UIView *vLineView5 = [[UIView alloc] init];
    vLineView5.backgroundColor = YAYIColor(180, 180, 180);
    [self.contentView addSubview:vLineView5];
    self.vLineView5 = vLineView5;
    
    UIView *hLineView2 = [[UIView alloc] init];
    hLineView2.backgroundColor = YAYIColor(180, 180, 180);
    [self.contentView addSubview:hLineView2];
    self.hLineView2 = hLineView2;
    
    UIView *hLineView3 = [[UIView alloc] init];
    hLineView3.backgroundColor = YAYIColor(180, 180, 180);
    [self.contentView addSubview:hLineView3];
    self.hLineView3 = hLineView3;
    
    UIView *vLineView6 = [[UIView alloc] init];
    vLineView6.backgroundColor = YAYIColor(180, 180, 180);
    [self.contentView addSubview:vLineView6];
    self.vLineView6 = vLineView6;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
//    CGFloat itemW = (SCREEN_width - 44) / 7.0;
    CGFloat itemH = self.contentView.bounds.size.height / 12;
    
    [self.morningSchedulingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(itemH);
        make.width.mas_equalTo(self.contentView);
        make.height.mas_equalTo(itemH * 4);
    }];
    
    [self.vLineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(0.5);
        make.height.mas_equalTo(itemH);
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(0);
    }];
    
    [self.hLineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.5);
        make.width.mas_equalTo(self.contentView);
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(0);
    }];
    
    [self.vLineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(0.5);
        make.height.mas_equalTo(itemH);
        make.right.mas_equalTo(self.contentView).offset(-0.0);
        make.top.mas_equalTo(0);
    }];
    
    [self.vLineView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(0.5);
        make.height.mas_equalTo(itemH);
        make.left.mas_equalTo(self.contentView);
        make.top.mas_equalTo(itemH * 5);
    }];
    
    [self.vLineView4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(0.5);
        make.height.mas_equalTo(itemH);
        make.right.mas_equalTo(self.contentView).offset(-0.0);
        make.top.mas_equalTo(itemH * 5);
    }];
    
    // 第二行
    [self.afternoonSchedulingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(itemH * 6);
        make.width.mas_equalTo(self.contentView);
        make.height.mas_equalTo(itemH * 4);
    }];
    
    // 底部线
    [self.vLineView5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(0.5);
        make.height.mas_equalTo(itemH * 2);
        make.left.mas_equalTo(0);
        make.bottom.mas_equalTo(self.contentView);
    }];
    
    [self.hLineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.5);
        make.width.mas_equalTo(self.contentView);
        make.left.mas_equalTo(0);
        make.bottom.mas_equalTo(self.contentView).offset(-0.0);
    }];
    
    [self.hLineView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.5);
        make.width.mas_equalTo(self.contentView);
        make.left.mas_equalTo(0);
        make.bottom.mas_equalTo(-itemH);
    }];
    
    [self.vLineView6 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(0.5);
        make.height.mas_equalTo(itemH * 2);
        make.right.mas_equalTo(self.contentView);
        make.bottom.mas_equalTo(self.contentView);
    }];
    
}

@end
